package com.efounder.message.struct;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;

public class IMStructFactory
{
	public static IMStruct getIMStruct(byte[] b)
	{
		ByteBuf buffer = Unpooled.buffer();
		buffer.writeBytes(b);
		return getIMStruct(buffer);
	}
	public static IMStruct getIMStruct(ByteBuf b)
	{
		IMStruct struct = null;
		byte messageType =b.readByte();
		switch((int)messageType)
		{
			case 0:
			{
				struct = new IMStruct000();
				break;
			}
			case 1:
			{
				struct = getIMStruct001(b);
				break;
			}
			case 2:
			{
				struct = getIMStruct002(b);
				break;
			}
		}
		return struct;
	}

	public static IMStruct getIMStruct001(ByteBuf b)
	{
		IMStruct001 struct = new IMStruct001();
		struct.setMsgLength(b.readInt());

		byte[] bs = new byte[b.readableBytes()];
		b.readBytes(bs);
		struct.setClientId(new String(bs));
		return struct;
	}
	public static IMStruct getIMStruct002(ByteBuf b)
	{
		IMStruct002 struct = new IMStruct002();
		struct.setMsgLength(b.readInt());
		struct.setFromUserId(b.readInt());
		struct.setToUserId(b.readInt());
		struct.setTime(b.readLong());
		struct.setMsgId(b.readInt());
		struct.setToUserType(b.readByte());
		struct.setMessageChildType(b.readByte());
		byte[] body = new byte[b.readInt()];
		b.readBytes(body);
		struct.setBody(body);
		return struct;
	}
}
